﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using NetJsWire.JsonRpcHandler;
using Newtonsoft.Json;

namespace NetJsWire.Widgets.Core
{
    /// <summary>
    ///   Ext core utilities and functions.
    /// </summary>
    public static class Ext
    {
        /// <summary>
        ///   Retrieves Ext.Element objects.
        /// </summary>
        /// <remarks>
        ///   <b>This method does not retrieve Components.</b> This method retrieves Ext.Element objects which encapsulate DOM elements. To retrieve a Component by its ID, use Ext.ComponentMgr.get.
        ///   Uses simple caching to consistently return the same object. Automatically fixes if an object was recreated with the same id via AJAX or DOM.
        ///   Shorthand of Ext.Element.get
        /// </remarks>
        /// <param name = "widget">An existing IWidget</param>
        /// <returns>A Javascript call to Ext.get wich returns a reference to your existing Element.</returns>
        public static string Get( this IWidget widget )
        {
            return string.Format( "Ext.get('{0}')", widget.Renderer.InternalID );
        }

        /// <summary>
        ///   This is shorthand reference to Ext.ComponentMgr.get. Looks up an existing Component by id
        /// </summary>
        /// <param name = "widget">An existing IWidget</param>
        /// <returns>A Javascript call to Ext.get wich returns the Component, <c>undefined</c> if not found, or <c>null</c> if a Class was found.</returns>
        public static string GetCmp( this IWidget widget )
        {
            return string.Format( "Ext.getCmp('{0}')", widget.Renderer.InternalID );
        }

        #region Nested type: Util

        /// <summary>
        ///   Various utility methods
        /// </summary>
        public static class Util
        {
            #region Nested type: Format

            /// <summary>
            ///   Formatting methods.
            /// </summary>
            public static class Format
            {
                /// <summary>
                ///   Parse a value into a formatted date using the specified format pattern.
                /// </summary>
                /// <param name = "date">The value to format</param>
                /// <param name = "format">Any valid date format string (defaults to 'm/d/Y')</param>
                /// <returns>The formatted date string</returns>
                public static string Date( DateTime date, string format )
                {
                    return string.Format( "Ext.util.format.date({0}, '{1}')", JsonConvert.ToString( date ), format ?? "m/d/Y" );
                }
            }

            #endregion
        }

        #endregion
    }
}
